<template>
  <div class="">
    <upload-excel :onSuccess="onSuccess" />
  </div>
</template>

<script setup>
import UploadExcel from '@/components/upload-excel'
import { USER_RELATIONS } from './utils'
import { userBatchImport } from '@/api/user-manage'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'
const router = useRouter()

const onSuccess = async ({ header, results }) => {
  console.log({ header, results })
  const data = generateData(results)
  await userBatchImport(data)
  ElMessage.success('数据更新成功')
  router.push('/user/manage')
}

/**
 * 格式化数据
 */
const generateData = (data) => {
  return data.map((row) => {
    const newRow = {}
    for (const [key, value] of Object.entries(row)) {
      newRow[USER_RELATIONS[key]] = value
    }
    return newRow
  })
}
</script>

<style lang="scss" scoped></style>
